home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1284 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  63 lines

  1. Path: news.th-darmstadt.de!news
  2. From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Question: references to variables within classes
  5. Date: Wed, 10 Jan 1996 09:58:36 +0100
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Message-ID: <30F37FBC.2781E494@intellektik.informatik.th-darmstadt.de>
  8. References: <DKy0MC.3nA@watserv3.uwaterloo.ca>
  9. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b4 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. Stephen Bay wrote:
  16. > Hi,
  17. > Is it possible in C++ to use variables within a class as parameters for a
  18. > constructor of an object contained within that class? I've tried compiling the
  19. > following code and my compiler chokes on the list iter declaration within the
  20. > class dodo - it doesn't recognize CityList and gives a syntax error. All the
  21. > other statements compile fine though.
  22. > -stephen
  23. > #include "slist.hpp"
  24. > NISList<int *>                  CityList;
  25. > NISListIter<int *>              CityListIter(CityList);
  26. > struct bird
  27. > {
  28. >         NISList<int *>                  CityList;
  29. >         NISListIter<int *>              CityListIter();
  30. > };
  31. > class   dodo
  32. > {
  33. >         public:
  34. >                 NISList<int *>                  CityList;
  35. > chokes here->   NISListIter<int *>              CityListIter(CityList);
  36. > };
  37. > main()
  38. > {
  39. >         NISList<int *>                  CityList;
  40. >         NISListIter<int *>              CityListIter(CityList);
  41. > };
  42.  
  43. You have to use the so-called member initializer list to pass the
  44. list-reference to the iterator, e.g.:
  45.  
  46.     class dodo {
  47.         public:
  48.                 NISList<int *>                  CityList;
  49.         NISListIter<int *>              CityListIter;
  50.  
  51.         dodo() : CityListIter(CityList) { ... }
  52.     };
  53.  
  54.     Enno
  55.